$(document).ready( function(){
	
	if($("#legal_person").attr("checked") == "checked")
	{
		$(".form-group.legal-person").show();
	}
	else
	{
		$(".form-group.legal-person").hide();
	}
	
	$("input[name=type]").on("change", function(){
		if($(this).val() == 0)
		{
			$(".form-group.legal-person").hide();
		}
		else
		{
			$(".form-group.legal-person").show();
		}
	});
	
	$("#country_id, #city_id, #county_id").select2();
	
	$("#country_id").on("change", function(){
		$("#ajaxLoader").show();
		var countryID = $(this).val();
		
		dataString = "&get_counties=1&country_id="+countryID;
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			success: function(response){
				$("#county_id").html(response).attr("disabled", false);
				$("#county_id").select2();
				$("#ajaxLoader").hide();
			}
		});
	});
	
	$("#county_id").on("change", function(){
		$("#ajaxLoader").show();
		var countyID = $(this).val();
		
		//var countryID = $("#country_id").val();
		
		dataString = "&get_cities=1&county_id="+countyID;
		//dataString += "&country_id="+countryID+";
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			success: function(response){
				$("#city_id").html(response).attr("disabled", false);
				$("#city_id").select2();
				$("#ajaxLoader").hide();
			}
		});
	});
	
	$("#registerForm button").on("click", function(){
		
		$("#ajaxLoader").show();
		
		var dataString = $("#registerForm").serialize();
		dataString += "&register=1";
		
		$.ajax({
				type:'POST',
				url: scriptsURL+"libs/ajax/ajax-includes.php",
				data: dataString,
				success: function(responseJSON)
				{
					var response = JSON.parse(responseJSON);
					var message = "";
					$("#ajaxLoader").hide();
					
					if(response.error_count && response.error_count > 0)
					{
						$.each( response, function( key, value ) {
							if($("#registerForm #"+key).length > 0)
							{
								$("#registerForm #"+key).parent().addClass("has-error");
								message += '<div class="alert alert-danger">'+value+'</div>';
							}
						});
						$("#messageDialog .modal-body").html(message);
						$('#messageDialog').modal('show');
						$("#messageDialog .btn").bind("click", function(){
							
						});
					}
					else
					{
						message = '<div class="alert alert-success">'+response.message+'</div>';
						
						$("#messageDialog .modal-body").html(message);
						$('#messageDialog').modal('show');
						$('#messageDialog').on('hidden.bs.modal', function (e) {
							$("#ajaxLoader").show();
							location.reload();
						});

					}
					
					
					
					$("#newCaptcha").trigger("click");
				}
		});
		return false;
	});
	$("#changePassword").on("click", function(){
		
		$("#passwordChangeDialog .btn.btn-danger").bind("click", function(){
			$("#ajaxLoader").show();
			var dataString = $("#passwordChangeDialog form").serialize();
			dataString += "&change_password=1";
			$.ajax({
				type:'POST',
				url: scriptsURL+"libs/ajax/ajax-includes.php",
				data: dataString,
				success: function(responseJSON)
				{
					response = JSON.parse(responseJSON);
					
					if(response.error_count > 0)
					{
						$("#passwordChangeDialog .modal-body .alert").html(response.message);
						$("#passwordChangeDialog .modal-body .alert").removeClass("hide");
					}
					else
					{
						$("#passwordChangeDialog .modal-body .alert").addClass("hide");
						$("#passwordChangeDialog").modal("hide");
						$("#messageDialog .modal-body").html("<div class='alert alert-success'>"+response.message+"</div>");
						$("#messageDialog").modal("show");
					}
					$("#ajaxLoader").hide();
					
					
				}
			});
			return false;
		});
		
		$('#passwordChangeDialog').modal('show');
		
		return false;
	});
	$('#changePassword').on('hidden.bs.modal', function (e) {
		$("#passwordChangeDialog .btn.btn-danger").unbind("click");
	});
	
});
